package com.sxj.shop.service.proxy;

import com.sxj.shop.exception.RequestParameterErrorException;
import com.sxj.shop.exception.UserNotLoginException;
import com.sxj.shop.factory.ObjectFactory;
import com.sxj.shop.service.CartService;
import com.sxj.shop.tran.TransactionManager;
import com.sxj.shop.vo.Cart;

/**
 * @author 石小俊
 * @date 2023年12月2023/12/12日 18:30
 */
public class CartServiceProxy implements CartService {

    private TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
    private CartService cartService = (CartService) ObjectFactory.getObject("cartServiceTarget");

    @Override
    public void addCart(Cart cart, String id) throws UserNotLoginException, RequestParameterErrorException {
        try {
            tran.begin();
            cartService.addCart(cart, id);
            tran.commit();
        } catch (UserNotLoginException e) {
            tran.rollback();
            throw e;
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void removeCart(Cart cart, String id) throws UserNotLoginException, RequestParameterErrorException {
        try {
            tran.begin();
            cartService.removeCart(cart, id);
            tran.commit();
        } catch (UserNotLoginException e) {
            tran.rollback();
            throw e;
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void modifyCart(Cart cart, String id, String num) throws UserNotLoginException, RequestParameterErrorException {
        try {
            tran.begin();
            cartService.modifyCart(cart, id, num);
            tran.commit();
        } catch (UserNotLoginException e) {
            tran.rollback();
            throw e;
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        }
    }
}
